//
//  ZLPopMenuItem.swift
//  ZLPopMenuSwift
//
//  Created by 科大 on 2025/1/21.
//

import UIKit

var ZLLineHeight: CGFloat  = 40
var ZLMaxWidth: CGFloat  = 200

public enum ZLZLPopMenuItemDirection {
    case horizontal  // 水平
    case vertical   // 竖直
}

public class ZLPopMenuItem: NSObject {
    
    /// 标题
    var title: String = ""
    /// 图片
    var image: UIImage?
    /// 颜色
    var color: UIColor = .black
    /// 字体
    var font: UIFont = .systemFont(ofSize: 14)
    /// 方向
    var direction: ZLZLPopMenuItemDirection = .horizontal
    /// 回调
    var callBack: () -> Void
    
    public init(title: String,
                image: UIImage? = nil,
                color: UIColor = .black,
                font: UIFont = .systemFont(ofSize: 14),
                direction: ZLZLPopMenuItemDirection = .horizontal,
                callBack: @escaping () -> Void) {
        self.title = title
        self.image = image
        self.color = color
        self.font = font
        self.direction = direction
        self.callBack = callBack
    }
    
    // MARK: - Size Calculation
    var itemSize: CGSize {
        let size = CGSize(width: CGFloat.greatestFiniteMagnitude, height: font.pointSize)
        let attributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: font.pointSize)]
        let rect = title.boundingRect(with: size,
                                      options: .usesLineFragmentOrigin,
                                      attributes: attributes,
                                      context: nil)
        
        if image != nil {
            return CGSize(width: ceil(rect.size.width) + 30, height: ZLLineHeight)
        }
        return CGSize(width: ceil(rect.size.width), height: ZLLineHeight)
    }
}
